package com.texaco.chapasFutbol.camera;

import org.andengine.engine.camera.ZoomCamera;
import org.andengine.input.touch.detector.ScrollDetector;
import org.andengine.input.touch.detector.ScrollDetector.IScrollDetectorListener;
import org.andengine.input.touch.detector.SurfaceScrollDetector;

public class ScrollHandler implements IScrollDetectorListener{

	private ZoomCamera camera;
	SurfaceScrollDetector scrollDetector;
	
	public ScrollHandler(ZoomCamera camera){
		this.camera = camera;
		scrollDetector = new SurfaceScrollDetector(this);
	}

	public void onScrollStarted(final ScrollDetector pScollDetector, final int pPointerID, final float pDistanceX, final float pDistanceY) {
		setOffsetCenter(pDistanceX, pDistanceY);
	}

	private void setOffsetCenter(final float pDistanceX, final float pDistanceY) {
		final float zoomFactor = camera.getZoomFactor();
		float xOff = -pDistanceX / zoomFactor;
		float yOff = -pDistanceY / zoomFactor;
		camera.offsetCenter(xOff, yOff);
	}

	public void onScroll(final ScrollDetector pScollDetector, final int pPointerID, final float pDistanceX, final float pDistanceY) {
		setOffsetCenter(pDistanceX, pDistanceY);
	}
	
	public void onScrollFinished(final ScrollDetector pScollDetector, final int pPointerID, final float pDistanceX, final float pDistanceY) {
		setOffsetCenter(pDistanceX, pDistanceY);
	}

	public SurfaceScrollDetector getScrollDetector() {
		return scrollDetector;
	}

	public void setScrollDetector(SurfaceScrollDetector scrollDetector) {
		this.scrollDetector = scrollDetector;
	}
}
